<template>
  <div class="app">
    <!---------- 查询表单form begin ----------->
    <a-form class="smart-query-form" ref="form">
      <a-row class="smart-query-form-row">
        <a-form-item label="电键" class="smart-query-form-item">
          <a-input style="width: 200px" v-model:value="queryForm.docEntry" placeholder="电键"  allowClear/>
        </a-form-item>
        <a-form-item label="标题" class="smart-query-form-item">
          <a-input style="width: 200px" v-model:value="queryForm.title" placeholder="标题"  allowClear/>
        </a-form-item>

        <a-form-item label="状态" class="smart-query-form-item">



          <a-select
              ref="select"
              v-model:value="queryForm.docStatus"
              style="width: 120px"
              allowClear
          >
            <a-select-option value="O">开启</a-select-option>
            <a-select-option value="C">关闭</a-select-option>

          </a-select>
        </a-form-item>

        <a-form-item class="smart-query-form-item">
          <a-button-group>
            <a-button type="primary" @click="onSearch">
              <template #icon>
                <SearchOutlined />
              </template>
              查询
            </a-button>
            <a-button @click="resetQuery" class="smart-margin-left10">
              <template #icon>
                <ReloadOutlined />
              </template>
              重置
            </a-button>
          </a-button-group>
        </a-form-item>
      </a-row>
    </a-form>
    <!---------- 查询表单form end ----------->

    <a-card size="small" :bordered="false" :hoverable="true">
      <!---------- 表格操作行 begin ----------->
      <a-row class="smart-table-btn-block">
        <a-button-group>
          <a-button @click="add" type="primary">
            <template #icon>
              <PlusOutlined />
            </template>
            新建
          </a-button>
        </a-button-group>

        <div class="smart-table-setting-block">
          <TableOperator v-model="columns" :tableId="null" :refresh="queryData" />
        </div>
      </a-row>
      <!---------- 表格操作行 end ----------->

      <!---------- 表格 begin ----------->
      <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="docEntry" :loading="tableLoading" :pagination="false">
        <template #bodyCell="{ text, record, column }">
          <template v-if="column.dataIndex === 'docEntry'">
            <a @click="onTo(text)">{{ text }} </a>
          </template>
        </template>
      </a-table>
      <!---------- 表格 end ----------->

      <div class="smart-query-table-page">
        <a-pagination
          showSizeChanger
          showQuickJumper
          show-less-items
          :pageSizeOptions="PAGE_SIZE_OPTIONS"
          :defaultPageSize="queryForm.pageSize"
          v-model:current="queryForm.pageNum"
          v-model:pageSize="queryForm.pageSize"
          :total="total"
          @change="queryData"
          @showSizeChange="queryData"
          :show-total="(total) => `共${total}条`"
        />
      </div>
    </a-card>
  </div>
</template>
<script setup>
  import { reactive, ref, onMounted } from 'vue';
  import { courseApi } from '/src/api/business/exam/course-api.js';

  import { PAGE_SIZE_OPTIONS } from '/src/constants/common-const';
  import { smartSentry } from '/src/lib/smart-sentry';
  import TableOperator from '/src/components/support/table-operator/index.vue';
  import { useRouter } from 'vue-router';
  import {columns} from "/@/views/business/exam/courseData/data.js";
  // ---------------------------- 表格列 ----------------------------
  const router = useRouter();

  const form = ref({})


  // ---------------------------- 查询数据表单和方法 ----------------------------

  const queryFormState = {
    pageNum: 1,
    pageSize: 20,
    docEntry: null, //电键
    title: null,
    docStatus: null,
    createId: null,
  };
  // 查询表单form
  const queryForm = reactive({ ...queryFormState });
  // 表格加载loading
  const tableLoading = ref(false);
  // 表格数据
  const tableData = ref([]);
  // 总数
  const total = ref(0);

  // 重置查询条件
  function resetQuery() {
    form.value.resetFields();
    let pageSize = queryForm.pageSize;
    Object.assign(queryForm, queryFormState);
    queryForm.pageSize = pageSize;
    queryData();
  }

  // 搜索
  function onSearch() {
    queryForm.pageNum = 1;
    queryData();
  }

  // 查询数据
  async function queryData() {
    tableLoading.value = true;
    try {
      let queryResult = await courseApi.queryPage(queryForm);
      tableData.value = queryResult.data.list;
      total.value = queryResult.data.total;
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      tableLoading.value = false;
    }
  }

  function onTo(item) {
    router.push({ path: 'coursea', query: { type: 'info', docEntry: item } });
  }

  function add() {
    router.push({ path: 'coursea', query: { type: 'new', docEntry: null } });
  }

  onMounted(queryData);
</script>
